home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / msdos / raytrace / dbwrend / source / tex.c < prev    next >
C/C++ Source or Header  |  1989-04-16  |  11KB  |  272 lines

  1. /************************************************************************
  2.  *                                                                      *
  3.  *                  Copyright (c) 1987, David B. Wecker                 *
  4.  *                          All Rights Reserved                         *
  5.  *                                                                      *
  6.  * This file is part of DBW_Render                                      *
  7.  *                                                                      *
  8.  * DBW_Render is distributed in the hope that it will be useful, but    *
  9.  * WITHOUT ANY WARRANTY. No author or distributor accepts               *
  10.  * responsibility to anyone for the consequences of using it or for     *
  11.  * whether it serves any particular purpose or works at all, unless     *
  12.  * he says so in writing. Refer to the DBW_Render General Public        *
  13.  * License for full details.                                            *
  14.  *                                                                      *
  15.  * Everyone is granted permission to copy, modify and redistribute      *
  16.  * DBW_Render, but only under the conditions described in the           *
  17.  * DBW_Render General Public License. A copy of this license is         *
  18.  * supposed to have been given to you along with DBW_Render so you      *
  19.  * can know your rights and responsibilities. It should be in a file    *
  20.  * named COPYING. Among other things, the copyright notice and this     *
  21.  * notice must be preserved on all copies.                              *
  22.  ************************************************************************
  23.  *                                                                      *
  24.  * Authors:                                                             *
  25.  *      DBW - David B. Wecker                                           *
  26.  *                                                                      *
  27.  * Versions:                                                            *
  28.  *      V1.0 870125 DBW - First released version                        *
  29.  *                                                                      *
  30.  ************************************************************************/
  31.  
  32. #define MODULE_TEXTURE
  33. #include "ray.h"
  34.  
  35. vector  white   = 
  36. {
  37.      1.0 ,1.0 ,1.0 
  38. }
  39. ;
  40.  
  41. void gettex(diffuse,np,p,n)
  42. vector  diffuse,
  43.   p,
  44.   n;
  45. node    *np;
  46. {
  47.      int         whichtexture,
  48.        i,
  49.        j,
  50.        ibx,
  51.        iby,
  52.        ibz;
  53.      float       texture,
  54.        bx,
  55.        by,
  56.        bz;
  57.      long        curs;
  58.  
  59.      /* keep track of maximum stack extent */
  60.      curs = curstack();
  61.      if (curs < stackbot)
  62.           stackbot = curs;
  63.  
  64.      whichtexture = np->attr.tex;
  65.  
  66.      /* This is copied to a temp so that local solid texturing may be applied
  67.      non-destructively. */
  68.  
  69.      veccopy(np->attr.dif,diffuse); /* start with the object's defined color */
  70.  
  71.      if (whichtexture != 0) 
  72.      { /* don't bother checking if there's no texture */
  73.           if (whichtexture >= 20 && whichtexture < 30) 
  74.           { /* MARBLE */
  75.                j = whichtexture - 20;  /* select the desired marble */
  76.                texture  = p[0] * marble[j].xscale;
  77.                texture += turbulence(p) * marble[j].turbscale;
  78.                texture  = (sin(texture) + 1.0) / 2.0;
  79.                for (i = 0; i < marble[j].squeeze; i++)
  80.                     texture = texture * texture;  /* squeeze peaks thinner */
  81.                blendcolor(diffuse,marble[j].veincolor,texture,diffuse);
  82.           }
  83.  
  84.           else if (whichtexture >= 40 && whichtexture < 50) 
  85.           { /* WOOD */
  86.                /*mulvm(p,woodorient,tp);*/
  87.                j = whichtexture - 40;  /* select the desired wood */
  88.                texture  = sqrt(p[0]*p[0] + p[2]*p[2]);
  89.                texture *= wood[j].ringspacing;
  90.                texture += turbulence(p) * wood[j].turbscale;
  91.                texture = (sin(texture) + 1.0) / 2.0;
  92.                for (i = 0; i < wood[j].squeeze; i++)
  93.                     texture = texture * texture;  /* squeeze peaks thinner */
  94.                texture *= wood[j].thickscale;  /* scale 0..n */
  95.                if (texture > 1.0)
  96.                     texture = 1.0;  /* clip to 0..1 */
  97.  
  98.                blendcolor(diffuse,wood[j].othercolor,texture,diffuse);
  99.           }
  100.  
  101.           else if (whichtexture >= 50 && whichtexture < 60) 
  102.           { /* CHECKERBOARD */
  103.                j = whichtexture - 50;  /* select the desired checker */
  104.                ibx = 0;
  105.                iby = 0;
  106.                ibz = 0;
  107.  
  108.                if (checker[j].x != 0.0) 
  109.                {
  110.                     texture = p[0] / checker[j].x;
  111.                     if (texture < 0.0)
  112.                          texture = 1.0 - texture;
  113.                     ibx = (int) texture;
  114.                }
  115.                if (checker[j].y != 0.0) 
  116.                {
  117.                     texture = p[1] / checker[j].y;
  118.                     if (texture < 0.0)
  119.                          texture = 1.0 - texture;
  120.                     iby = (int) texture;
  121.                }
  122.                if (checker[j].z != 0.0) 
  123.                {
  124.                     texture = p[2] / checker[j].z;
  125.                     if (texture < 0.0)
  126.                          texture = 1.0 - texture;
  127.                     ibz = (int) texture;
  128.                }
  129.  
  130.                if ((ibx+iby+ibz) & 1) 
  131.                {
  132.                     veccopy(checker[j].color,diffuse);
  133.                }
  134.           }
  135.  
  136.           else if (whichtexture >= 70 && whichtexture < 80) 
  137.           { /* BLEND */
  138.                j = whichtexture - 70;  /* select the desired blend */
  139.                texture = (p[1] - blend[j].start) / blend[j].scale;
  140.                if (texture > 1.0)
  141.                     texture = 1.0;
  142.                if (texture < 0.0)
  143.                     texture = 0.0;
  144.                blendcolor(diffuse,blend[j].color,texture,diffuse);
  145.           }
  146.  
  147.           else if (whichtexture >= 80 && whichtexture < 90) 
  148.           { /* SNOW */
  149.                j = whichtexture - 80;  /* select the desired snow */
  150.                texture  = p[1] - snow[j].start;
  151.                texture /= snow[j].altscale;
  152.                texture *= snow[j].altfactor;
  153.                texture *= DOT(n,yaxis);
  154.                if (texture > 1.0)
  155.                     texture = 1.0;
  156.                if (texture < 0.0)
  157.                     texture = 0.0;
  158.                if (texture > snow[j].threshhold)
  159.                     texture = 1.0;
  160.                blendcolor(diffuse,white,texture,diffuse);
  161.           }
  162.  
  163.           else
  164.                switch (whichtexture) 
  165.                { /* MISC SINGULAR TEXTURES */
  166.                case 1  :
  167.                     /* Y axis color gradiation */
  168.                     hls(p[1]/20.0,0.5,1.0,diffuse);
  169.                     break;
  170.  
  171.                case 3  :
  172.                     /* mottled diffuse */
  173.                     vecscale(noise(p),diffuse,diffuse);
  174.                     break;
  175.  
  176.                case 5  :
  177.                     /* Brick pattern texture */
  178.  
  179.                     /* First,check mortar layers in the X-Z plane */
  180.                     by  = p[1] / brickheight;
  181.                     iby = (int) by;
  182.                     by -= (float) iby; /* just save fraction */
  183.                     if (by < 0.0)
  184.                          by += 1.0;
  185.                     if (by <= brickmortarheight) 
  186.                     {
  187.                          /* This point is on the X-Z plane */
  188.                          veccopy(mortardiffuse,diffuse);
  189.                          break;
  190.                     }
  191.  
  192.                     /* Check odd mortar layers in the Y-Z plane */
  193.                     /* First calculate a common factor */
  194.                     by  = p[1];
  195.                     by /= brickheight;
  196.                     by /= 2.0;
  197.                     iby = (int) by;
  198.                     by -= (float) iby; /* just save fraction */
  199.                     if (by < 0.0)
  200.                          by += 1.0;
  201.  
  202.                     bx  = p[0] / brickwidth;
  203.                     ibx = (int) bx;
  204.                     bx -= (float) ibx; /* just save fraction */
  205.                     if (bx < 0.0)
  206.                          bx += 1.0;
  207.                     if (bx <= brickmortarwidth) 
  208.                     {
  209.                          /* So this point is on the Y-Z mortar plane.  We now must
  210.                          factor in the odd staggered brick effect */
  211.                          if (by <= 0.5) 
  212.                          {
  213.                               veccopy(mortardiffuse,diffuse);
  214.                               break;
  215.                          }
  216.                     }
  217.                     /* Check even mortar layers in the Y-Z plane */
  218.                     bx  = (p[0] / brickwidth) + 0.5;
  219.                     ibx = (int) bx;
  220.                     bx -= (float) ibx; /* just save fraction */
  221.                     if (bx < 0.0)
  222.                          bx += 1.0;
  223.                     if (bx <= brickmortarwidth) 
  224.                     {
  225.                          /* So this point is on the Y-Z mortar plane.  We now must
  226.                          factor in the even staggered brick effect */
  227.                          if (by > 0.5) 
  228.                          {
  229.                               veccopy(mortardiffuse,diffuse);
  230.                               break;
  231.                          }
  232.                     }
  233.  
  234.                     /* Check odd mortar layers in the Y-X plane */
  235.                     bz  = p[2] / brickdepth;
  236.                     ibz = (int) bz;
  237.                     bz -= (float) ibz; /* just save fraction */
  238.                     if (bz < 0.0)
  239.                          bz += 1.0;
  240.                     if (bz <= brickmortardepth) 
  241.                     {
  242.                          /* So this point is on the Y-X mortar plane.  We now must
  243.                          factor in the odd staggered brick effect */
  244.                          if (by > 0.5) 
  245.                          {
  246.                               veccopy(mortardiffuse,diffuse);
  247.                               break;
  248.                          }
  249.                     }
  250.                     /* Check even mortar layers in the Y-X plane */
  251.                     bz  = (p[2] / brickdepth) + 0.5;
  252.                     ibz = (int) bz;
  253.                     bz -= (float) ibz; /* just save fraction */
  254.                     if (bz < 0.0)
  255.                          bz += 1.0;
  256.                     if (bz <= brickmortardepth) 
  257.                     {
  258.                          /* So this point is on the Y-X mortar plane.  We now must
  259.                          factor in the even staggered brick effect */
  260.                          if (by <= 0.5) 
  261.                          {
  262.                               veccopy(mortardiffuse,diffuse);
  263.                               break;
  264.                          }
  265.                     }
  266.                     /* So we're inside a brick */
  267.                     break;
  268.                }
  269.      }
  270. }
  271.  
  272.